#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define inf 1000000007
#define N 3010
#define M 8010
using namespace std;
int f[N],w[N];
int belong[N],low[N],dfn[N],vis[N],s[N];
int next[M],head[N],to[M],b[N];
int sum,top,u,v,r;
int n,p,ans;
int getint()
{
	int res=0,w=1;
	char ch=getchar();
	while((ch>'9' || ch<'0')&&ch!='-')
		ch=getchar();
	if(ch=='-')
	{
		w=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') 
	{
		res=res*10+ch-'0';
		ch=getchar();
	}
	return res*w;
}
void link(int x,int y)
{
	next[++sum]=head[x];
	head[x]=sum;
	to[sum]=y;
	f[y]++;
}
void tarjan(int p)
{
	low[p]=dfn[p]=++sum;
	s[++top]=p;
	vis[p]=true;
	for(int i=head[p];i;i=next[i])
	{
		int vv=to[i];
		if(!dfn[vv])
		{
			tarjan(vv);
			low[p]=min(low[p],low[vv]);
		}
		else if(vis[vv])
			low[p]=min(low[p],dfn[vv]);
	}
	if (dfn[p]==low[p])
	{
		int q=0;
		memset(b,0,sizeof(b));
		for(int j=top-1;j;j--)
			if (p==s[j])
			{
				b[s[j]]=1;
				q+=f[s[j]];
				break;
			}
			else
			{
				b[s[j]]=1;
				q+=f[s[j]];	
			}
			do
			{
				v=s[top--];
				vis[v]=false;
				belong[v]=p;
				for(int j=head[v];j;j=next[j])
					if(b[to[j]])
					{
						q--;
						f[to[j]]--;
					}
				if((w[v]&&w[v]<w[p])||!w[p])
					w[p]=w[v]; 
			}
			while(p!=v);
			f[p]+=q;
	}
}
int main()
{
	n=getint();
	p=getint();
	for(int i=1;i<=p;i++)
	{
		u=getint();
		v=getint();
		w[u]=v;
	}
	r=getint();
	for(int i=1;i<=r;i++)
	{
		u=getint();
		v=getint();
		link(u,v);
	}
	sum=0;
	for(int i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i);
	for(int i=1;i<=n;i++)
		if(f[belong[i]]<=0)
		{
			if(!w[belong[i]])
			{
				printf("NO\n%d\n",i);
				exit(0);
			}
			ans+=w[belong[i]];
			f[belong[i]]=1;
		}
	printf("YES\n%d\n",ans);
	return 0;
}
